package com.example.hellospringbootstarterautoconfigure.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @EnableConfigurationProperties(ServerProperties.class)
 * 启用指定类的（这里是 HelloService 类）的 @ConfigurationProperties注解功能(@ConfigurationProperties注解功能)；
 * 将配置类中对应的值和 HelloProperties 绑定起来；并把 HelloProperties 对象加入到IOC容器中
 */
@Configuration
@ConditionalOnMissingBean(HelloService.class) // 容器中没有 HelloService 对象的时候，才自动配置
@EnableConfigurationProperties(HelloProperties.class)

public class HelloServiceAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

    /**
     * 构造器的参数会自动注入，当只有一个构造器的时候
     * @param helloProperties
     * @return
     */
    @Bean
    public HelloService helloService(HelloProperties helloProperties){
        return new HelloService(helloProperties);
    }

}
